import 'package:safetymonitor/app/core/core.dart';

/// 自定义卡片（高频组件）
///
/// eg:
/// ```dart
/// CustomCard(
///   title: '标题',
///   content: Text('内容'),
///   divider: CustomCard.builtinDividerWithPadding,
///   onTap: () {},
///   titleRight: Icon(Icons.arrow_forward_ios),
///   color: Colors.white,
/// )
/// ```
class CustomCardX extends StatelessWidget {
  const CustomCardX({
    required this.child,
    this.title,
    super.key,
    this.titleWidget,
    this.padding,
    this.radius,
    this.divider,
    this.onTap,
    this.titleRight,
    this.color,
    this.margin,
  });

  /// 标题 String，优先级低于 [titleWidget]，如果 [titleWidget] 不为空，则 [title] 无效
  final String? title;

  /// 标题 Widget，优先级高于 [title]，使用此属性时 [title] 无效
  final Widget? titleWidget;

  /// 标题右侧 Widget，无论 [title] 还是 [titleWidget] 为何值，都会显示在右侧
  final Widget? titleRight;

  /// 内容，如果有 [title] 显示在 [title] 下方，如果有 [divider]，则显示在 [divider] 下方
  final Widget child;

  /// 卡片整体的内边距
  final EdgeInsetsGeometry? padding;

  /// 外边距
  final EdgeInsetsGeometry? margin;

  /// 边角弧度
  final double? radius;

  /// 标题与内容间的分割线
  final Widget? divider;

  /// 点击回调，点击整个卡片都会生效
  final VoidCallback? onTap;

  /// 卡片颜色
  final Color? color;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: padding ?? EdgeInsets.symmetric(horizontal: 12.w, vertical: 14.w),
        decoration: BoxDecoration(
          color: color ?? Colors.white,
          borderRadius: BorderRadius.circular((radius ?? 6).w),
        ),
        margin: margin,
        child: Column(
          children: [
            if (titleWidget != null) titleWidget ?? AppGaps.zero,
            if (title != null && titleWidget == null)
              Row(
                children: [
                  Container(
                    height: 12.w,
                    width: 3.w,
                    decoration: BoxDecoration(
                      color: Colors.blue,
                      borderRadius: BorderRadius.circular(6.w),
                    ),
                  ),
                  AppGaps.hGap10,
                  Expanded(
                    child: Text(
                      title ?? '',
                      style: TextStyle(
                        fontSize: 16.sp,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                  if (titleRight != null) titleRight ?? AppGaps.zero,
                ],
              ),
            if (title != null || titleWidget != null) divider ?? AppGaps.vGap12,
            child,
          ],
        ),
      ),
    );
  }

  /// 内置分割线，没有上下边距
  static const builtinDivider = Divider(
    height: 1,
    color: AppColors.divider_color_blue,
  );

  /// 内置分割线，有上下边距
  static final builtinDividerWithPadding = Padding(
    padding: EdgeInsets.symmetric(vertical: 12.w),
    child: builtinDivider,
  );

  /// 内置，标题前面的小圆点
  static final Widget leadingSmall = Container(
    decoration: BoxDecoration(
      gradient: const LinearGradient(
        colors: [Color(0xFF5C6DFF), Color(0xFFB0ABFB)],
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
      ),
      borderRadius: BorderRadius.circular(3.w),
    ),
    height: 12.w,
    width: 4.w,
  );

  static final Widget leadingVerticalRect = Container(
    height: 12.w,
    width: 3.w,
    decoration: BoxDecoration(
      borderRadius: BorderRadius.circular(1.w),
      gradient: const LinearGradient(
        colors: [
          Color(0xff6DADFF),
          Color(0xff2267C0),
        ],
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
      ),
    ),
  );
}
